﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TNet
{
    class Program
    {
        static void Main(string[] args)
        {
            new ChannelManager();
            ChannelManager.Instance.Init(); // 初始化

            ChannelManager.Instance.Connect(ServerType.LoginServer, "127.0.0.1", 12345);
            ChannelManager.Instance.Register<ConnectMsg>((ushort)Packet.Connect, OnConnectResoult1);

            while (true)
            {
                ChannelManager.Instance.UpdateMainThread();
            }

            ChannelManager.Instance.Release();
        }

        static void OnConnectResoult2(ConnectMsg msgType)
        {
            Console.WriteLine("ip:{0} prot:{1} result:{2}", msgType.ip, msgType.prot, msgType.bSucc);
            Buffer buffer = Buffer.Create();
            BinaryWriter Writer = buffer.BeginWriting(false);
//            BinaryHandle.WriteString(Writer, "192.168.1.128");
            BinaryHandle.Write7BitEncodedInt(Writer, 1);
            BinaryHandle.Write7BitEncodedInt(Writer, 2);
            BinaryHandle.Write7BitEncodedInt(Writer, 3);
//             BinaryHandle.WriteInt(Writer, 1);
//             BinaryHandle.WriteInt(Writer, 2);
//             BinaryHandle.WriteInt(Writer, 3);
            ChannelManager.Instance.SendPacket(ServerType.LoginServer, buffer);
        }

        static void OnConnectResoult1(ConnectMsg msgType)
        {
            Console.WriteLine("ip:{0} prot:{1} result:{2}", msgType.ip, msgType.prot, msgType.bSucc);
            Buffer buffer = Buffer.Create();
            LoginData loginData = new LoginData();
            loginData.c = new LoginData.client();
            loginData.c.ip = "192.168.1.128";
            loginData.c.valueList = new System.Collections.Generic.List<int>();
            loginData.c.valueList.Add(1);
            loginData.c.valueList.Add(2);
            loginData.c.valueList.Add(3);

            loginData.ssints = new System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.List<int>>>();
            loginData.clientmap = new Dictionary<LoginData.client, string>();
            loginData.clientmap[loginData.c] = "这是为什么呢?";
            loginData.unit1 = 1000;
            loginData.char1 = 126;
            loginData.uchar1 = 250;
            loginData.short1 = 2500;
            loginData.ushort1 = 5000;

            loginData.WriteToBinary(buffer.BeginWriting(false));
            ChannelManager.Instance.SendPacket(ServerType.LoginServer, buffer);
        }

        static void OnConnectResoult(ConnectMsg msgType)
        {
            Console.WriteLine("ip:{0} prot:{1} result:{2}", msgType.ip, msgType.prot, msgType.bSucc);

            Buffer buffer = Buffer.Create();
            BinaryWriter binary = buffer.BeginWriting(false);
            string text = "";
            for (int i = 0; i < 12; ++i)
                text += "1234567890";
            text += "1234567";

            binary.Write(text);
            ChannelManager.Instance.SendPacket(ServerType.LoginServer, buffer);
        }
    }
}
